﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Xml;

namespace iControl
{
	[ValueConversion(typeof(XmlAttribute), typeof(string))]
	public class KeyMapValueConverter: IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			string retVal = GetEquivalentCommandText(null);

			if( value != null )
			{
				try
				{
					XmlAttribute attrib = (XmlAttribute)value;
					ControlCommand command = (ControlCommand)Enum.Parse(typeof(ControlCommand), attrib.Value);
					retVal = GetEquivalentCommandText(command);
				}
				catch( Exception ex )
				{
					System.Diagnostics.Debug.WriteLine(ex.Message);
					System.Diagnostics.Debug.WriteLine(ex.StackTrace);
				}
			}

			return retVal;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			// we don't intend this to ever be called
			return null;
		}

		/// <summary>
		/// Gets equivalent name for specified command.
		/// </summary>
		public static string GetEquivalentCommandText(ControlCommand? command)
		{
			if( command != null )
			{
				switch( command )
				{
					case ControlCommand.None:
						return Properties.Resources.CommandControl_None;
					case ControlCommand.Play:
						return Properties.Resources.CommandControl_Play;
					case ControlCommand.PlayPause:
						return Properties.Resources.CommandControl_PlayPause;
					case ControlCommand.Stop:
						return Properties.Resources.CommandControl_Stop;
					case ControlCommand.NextTrack:
						return Properties.Resources.CommandControl_NextTrack;
					case ControlCommand.BackTrack:
						return Properties.Resources.CommandControl_BackTrack;
					case ControlCommand.PreviousTrack:
						return Properties.Resources.CommandControl_PreviousTrack;
                    case ControlCommand.NextAlbum:
                        return Properties.Resources.CommandControl_NextAlbum;
                    case ControlCommand.PreviousAlbum:
                        return Properties.Resources.CommandControl_PreviousAlbum;
					case ControlCommand.ShowHideCurrentTrackInfo:
						return Properties.Resources.CommandControl_ShowHideCurrentTrackInfo;
					case ControlCommand.ToggleShuffle:
						return Properties.Resources.CommandControl_ToggleShuffle;
					case ControlCommand.ToggleAllSongsRepeat:
						return Properties.Resources.CommandControl_ToggleAllSongsRepeat;
					case ControlCommand.FastForward:
						return Properties.Resources.CommandControl_FastForward;
					case ControlCommand.Rewind:
						return Properties.Resources.CommandControl_Rewind;
					case ControlCommand.Rate0:
						return Properties.Resources.CommandControl_Rate0;
					case ControlCommand.Rate1:
						return Properties.Resources.CommandControl_Rate1;
					case ControlCommand.Rate2:
						return Properties.Resources.CommandControl_Rate2;
					case ControlCommand.Rate3:
						return Properties.Resources.CommandControl_Rate3;
					case ControlCommand.Rate4:
						return Properties.Resources.CommandControl_Rate4;
					case ControlCommand.Rate5:
						return Properties.Resources.CommandControl_Rate5;
					case ControlCommand.IncreaseVolume:
						return Properties.Resources.CommandControl_IncreaseVolume;
					case ControlCommand.DecreaseVolume:
						return Properties.Resources.CommandControl_DecreaseVolume;
					case ControlCommand.ToggleMute:
						return Properties.Resources.CommandControl_ToggleMute;
					case ControlCommand.IncreaseMasterVolume:
						return Properties.Resources.CommandControl_IncreaseMasterVolume;
					case ControlCommand.DecreaseMasterVolume:
						return Properties.Resources.CommandControl_DecreaseMasterVolume;
					case ControlCommand.ToggleMuteMasterVolume:
						return Properties.Resources.CommandControl_ToggleMuteMasterVolume;
					case ControlCommand.OpenSettingsWindow:
						return Properties.Resources.CommandControl_OpenSettingsWindow;
                    case ControlCommand.OpenQuickSelectWindow:
                        return Properties.Resources.CommandControl_OpenQuickSelectWindow;
                    case ControlCommand.OpenAboutWindow:
                        return Properties.Resources.CommandControl_OpenAboutWindow;
					case ControlCommand.Quit:
						return Properties.Resources.CommandControl_Quit;
				}
			}
			return Properties.Resources.CommandControl_Error;
		}
	}
} 
